﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DietPlanning.Models;

namespace DietPlanning.Controllers
{ 
    public class DishDetailController : Controller
    {
        private DailyFoodEntities db = new DailyFoodEntities();

        //
        // GET: /DishDetail/

        public ViewResult Index()
        {
            var dishdetails = db.DishDetails.Include("Dish").Include("Food");
            return View(dishdetails.ToList());
        }

        //
        // GET: /DishDetail/Details/5

        public ViewResult Details(int id)
        {
            DishDetail dishdetail = db.DishDetails.Single(d => d.DishDetailID == id);
            return View(dishdetail);
        }

        //
        // GET: /DishDetail/Create

        public ActionResult Create()
        {
            ViewBag.DishID = new SelectList(db.Dishes, "DishID", "DishName");
            ViewBag.FoodID = new SelectList(db.Foods, "FoodID", "FoodName");
            return View();
        } 

        //
        // POST: /DishDetail/Create

        [HttpPost]
        public ActionResult Create(DishDetail dishdetail)
        {
            if (ModelState.IsValid)
            {
                db.DishDetails.AddObject(dishdetail);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.DishID = new SelectList(db.Dishes, "DishID", "DishName", dishdetail.DishID);
            ViewBag.FoodID = new SelectList(db.Foods, "FoodID", "FoodName", dishdetail.FoodID);
            return View(dishdetail);
        }
        
        //
        // GET: /DishDetail/Edit/5
 
        public ActionResult Edit(int id)
        {
            DishDetail dishdetail = db.DishDetails.Single(d => d.DishDetailID == id);
            ViewBag.DishID = new SelectList(db.Dishes, "DishID", "DishName", dishdetail.DishID);
            ViewBag.FoodID = new SelectList(db.Foods, "FoodID", "FoodName", dishdetail.FoodID);
            return View(dishdetail);
        }

        //
        // POST: /DishDetail/Edit/5

        [HttpPost]
        public ActionResult Edit(DishDetail dishdetail)
        {
            if (ModelState.IsValid)
            {
                db.DishDetails.Attach(dishdetail);
                db.ObjectStateManager.ChangeObjectState(dishdetail, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DishID = new SelectList(db.Dishes, "DishID", "DishName", dishdetail.DishID);
            ViewBag.FoodID = new SelectList(db.Foods, "FoodID", "FoodName", dishdetail.FoodID);
            return View(dishdetail);
        }

        //
        // GET: /DishDetail/Delete/5
 
        public ActionResult Delete(int id)
        {
            DishDetail dishdetail = db.DishDetails.Single(d => d.DishDetailID == id);
            //return View(dishdetail);
            db.DishDetails.DeleteObject(dishdetail);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /DishDetail/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            DishDetail dishdetail = db.DishDetails.Single(d => d.DishDetailID == id);
            db.DishDetails.DeleteObject(dishdetail);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}